package development;

import java.util.Vector;

import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.Graphics;
import state.Key;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import development.bean.Item;
import development.bean.Notification;
import development.group.FrmGroupThoiTietTinTuc;

public class FrmNotification extends GameForm{
  private final String ADV_TYPE = "ADV";
  private final String EVENT_TYPE = "EVENT";
  private final String TEXT_TYPE = "TEXT";
  private final String IMAGE_TYPE = "IMAGE";
  private final String OK_TYPE = "OK_TYPE";
  private final String YES_NO_TYPE = "YES_NO_TYPE";
  
  public static final int TIMER_DELAY = 1000; // 1 giây
  private int timeTemp;
  private Notification notification;
  private Vector items;
  private Item item;
  private ImageText text11;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmNotification();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    text11 = FontManager.getFont(FontManager.FONT_SIZE_11);
    notification = (Notification) parameters.get("notification");
    items = notification.items;
    if (notification.code.startsWith(ADV_TYPE)) {
      this.setSize(notification.SIZE_X, notification.SIZE_Y);
      this.setPosition(notification.POSITION_X, notification.POSITION_Y);
      Global.systemCanvas.masterForm = FrmNotification.instance();
      Global.systemCanvas.masterForm.isRunning = true;
    }
    if (notification.code.startsWith(EVENT_TYPE)) {
      item = (Item) items.elementAt(0);
      String type = item.type;
      String content = item.content;
      int alertId = item.id;
      byte alertType = 0;
      if (type.equals(OK_TYPE)) {
        alertType = GameAlert.OK_TYPE;
      } else if (type.equals(YES_NO_TYPE)) {
        alertType = GameAlert.YES_NO_TYPE;
      }
      Global.alert.showAlert1(this, alertType, GameStringUtils.split(content,"@@@"), alertId);
    }
    Global.setTimerDelay(TIMER_DELAY);
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    if(timeTemp >= notification.timeAlive){
      Global.systemCanvas.masterForm.isRunning = false;
      Global.systemCanvas.masterForm = null;
      timeTemp = 0;
      text11 = null;
      return;
    }
    if (notification.code.startsWith(ADV_TYPE)) {
      g.drawImage(notification.bgImage, notification.POSITION_X, notification.POSITION_Y, GameConstants.TOP_LEFT_ANCHOR);
      for (int i = 0; i < items.size(); i++) {
        item = (Item) items.elementAt(i);
        if (item.type.equals(TEXT_TYPE)) {
          item.imageText.drawString2(g, item.content, item.color, item.screen_240x320_x, item.screen_240x320_y,
              item.screen_240x320_anchor);
        } else if (item.type.equals(IMAGE_TYPE)) {
          g.drawImage(item.image, item.screen_240x320_x, item.screen_240x320_y, item.screen_240x320_anchor);
        }
      }
      text11.drawString2(g, "X Đóng", GameColor.RED2_CODE, notification.SIZE_X, notification.SIZE_Y, GameConstants.BOTTOM_RIGHT_ANCHOR);
    }
    timeTemp = timeTemp + TIMER_DELAY;
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == item.id) {
      if (alertType == GameAlert.YES_NO_TYPE) {
        if (eventType == GameAlert.YES_BUTTON) {
          forward(item.forwardUrl);
        }
      } else if (eventType == GameAlert.NO_BUTTON || alertType == GameAlert.OK_TYPE && eventType == GameAlert.OK_BUTTON) {
        return;
      }
    }
  }

  private void forward(String uri) {
    if (uri == null || uri.equals("")) {
      return;
    }
    if (uri.toUpperCase().startsWith("HTTP://")) {
      Global.systemCanvas.getMidlet().platformRequest(uri);
    } else {
      FrmGroupThoiTietTinTuc.tabIndex = 2;
      Global.nextState(FrmGroupThoiTietTinTuc.instance(), null);
    }
  }

  public void keyReleased(int keyCode) {
    
    switch (keyCode) {
    case Key.FIRE:
    case Key.K_5:
      forward(item.forwardUrl);
      break;
    case Key.SOFT_RIGHT:
      Global.systemCanvas.masterForm.isRunning = false;
      Global.systemCanvas.masterForm = null;
      break;
    case Key.SOFT_LEFT:
      break;
    }
  }
  
  public void destroy() {
    notification = null;
    items = null;
    item = null;
    text11 = null;
  }
}
